﻿using System;
using System.Collections.Generic;
using System.Text;
using Mighty.Common;

namespace Mighty.Brain
{
    /// <summary>
    /// 개인별 추측정보. 
    /// </summary>
    public class BossGuess
    {
        public UsedType[,] usedCard = new UsedType[4, 15];
        public UsedType usedJoker = new UsedType();

        public float[,] probCard = new float[4, 15];    // 확률
        public float probJoker;                         // 확률

        public float[] extShape = new float[4];         // 무늬기대값

        public int[] cntShapeUnknown = new int[4];
        public int[] cntShapeNone = new int[4];
        public int[] cntShapeHave = new int[4];
        public int[] cntShapeUsed = new int[4];
        public int cntUnknown = 0;
        public int cntNone = 0;
        public int cntHave = 0;
        public int cntUsed = 0;
        public PlayerType playerType = PlayerType.DependPlayer;

        // cnt...의 변수들을 update한다. 단 joker는 포함되지 않는다
        public void UpdateCnt()
        {
            cntUnknown = 0;
            cntNone = 0;
            cntHave = 0;
            cntUsed = 0;

            for (int j = 0; j < 4; j++)
            {
                cntShapeUnknown[j] = 0;
                cntShapeNone[j] = 0;
                cntShapeHave[j] = 0;
                cntShapeUsed[j] = 0;

                for (int k = 2; k < 15; k++)
                {
                    if (usedCard[j, k] == UsedType.unknown) cntShapeUnknown[j]++;
                    else if (usedCard[j, k] == UsedType.none) cntShapeNone[j]++;
                    else if (usedCard[j, k] == UsedType.have) cntShapeHave[j]++;
                    else if (usedCard[j, k] == UsedType.used) cntShapeUsed[j]++;
                }
            }

            for (int j = 0; j < 4; j++)
            {
                cntUnknown += cntShapeUnknown[j];
                cntNone += cntShapeNone[j];
                cntHave += cntShapeHave[j];
                cntUsed += cntShapeUsed[j];
            }
            if (usedJoker == UsedType.unknown) cntUnknown++;
            else if (usedJoker == UsedType.none) cntNone++;
            else if (usedJoker == UsedType.have) cntHave++;
            else if (usedJoker == UsedType.used) cntUsed++;

        }

        public Shape GetMaxShape(UsedType u, Shape skip)
        {

            int[] cntShape = null;
            if (u == UsedType.unknown) cntShape = cntShapeUnknown;
            else if (u == UsedType.none) cntShape = cntShapeNone;
            else if (u == UsedType.have) cntShape = cntShapeHave;
            else if (u == UsedType.used) cntShape = cntShapeUsed;

            int pos = -1;
            int max = 0;
            for (int i = 0; i < 4; ++i)
            {
                if (i == (int)skip) continue;
                if (cntShape[i] >= max)
                {
                    max = cntShape[i];
                    pos = i;
                }
            }
            return (Shape)max;
        }

        public Shape GetMinShape(UsedType u, Shape skip)
        {

            int[] cntShape = null;
            if (u == UsedType.unknown) cntShape = cntShapeUnknown;
            else if (u == UsedType.none) cntShape = cntShapeNone;
            else if (u == UsedType.have) cntShape = cntShapeHave;
            else if (u == UsedType.used) cntShape = cntShapeUsed;

            int pos = -1;
            int min = 14;
            for (int i = 0; i < 4; ++i)
            {
                if (i == (int)skip) continue;

                if (cntShape[i] <= min)
                {
                    min = cntShape[i];
                    pos = i;
                }
            }
            return (Shape)min;
        }
    }
}
